//
//  CustomTimePicker.m
//  MicrowaveInteractionLight
//
//  Created by maple on 2016/12/16.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "CustomTimePicker.h"

@interface CustomTimePicker ()

@property(nonatomic,strong)NSMutableArray *hourMutableArray;
@property(nonatomic,strong)NSMutableArray *minuteMutableArray;
@property(nonatomic,copy)NSString *mySelectedHourString;
@property(nonatomic,copy)NSString *mySelectedMinuteString;
@end

@implementation CustomTimePicker

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame])
    {
        self.hourMutableArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 24; i++) {
            [self.hourMutableArray addObject:@(i).stringValue];
        }
        
        self.minuteMutableArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 60; i++) {
            [self.minuteMutableArray addObject:@(i).stringValue];
        }
        
        _myPickerView = [[UIPickerView alloc]initWithFrame:self.bounds];
        _myPickerView.delegate = self;
        [self addSubview:_myPickerView];
        
        UILabel *houLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0, 216/2-30+1, 30, 60)];
        houLabel.text = @"时";
        houLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:houLabel];
        
        UILabel *minLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-70, 216/2-30+1, 30, 60)];
        minLabel.text = @"分";
        minLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:minLabel];

    }
    
    return self;
}

#pragma mark -- setter
- (void)setSelectedTimeString:(NSString *)selectedTimeString
{
    _selectedTimeString = selectedTimeString;
    //设置初始默认值
    NSArray *myDataArray = [selectedTimeString componentsSeparatedByString:@":"];
    self.mySelectedHourString = myDataArray[0];
    self.mySelectedMinuteString = myDataArray[1];
    [_myPickerView selectRow:[myDataArray[0] integerValue] inComponent:0 animated:NO];
    [_myPickerView selectRow:[myDataArray[1] integerValue] inComponent:1 animated:NO];
}

 #pragma Mark -- UIPickerViewDataSource
 // pickerView 列数
 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
 
    return 2;
 }
 
 // pickerView 每列个数
 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
     if (component == 0)
     {
     return self.hourMutableArray.count;
     }
     else if (component == 1)
     {
         return self.minuteMutableArray.count;
     }
     return 0;
 }

 // 每列宽度
 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    
    return 200;
 
}
 -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
 {
     return 50;
 }
 // 返回选中的行
 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
 {
     if (component == 0)
     {
         self.mySelectedHourString = self.hourMutableArray[row];
     }
     else if (component == 1)
     {
         self.mySelectedMinuteString = self.minuteMutableArray[row];
     }
     
     if (_delegate && [_delegate respondsToSelector:@selector(selectedCustomPickerTimeString:)])
     {
         [_delegate selectedCustomPickerTimeString:[NSString stringWithFormat:@"%@%@",self.mySelectedHourString,self.mySelectedMinuteString]];
     }
 }
 -(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
 {
 
     UIView *views = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/2, 60)];
     UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 1, SCREEN_WIDTH/2, 60)];
     lable.font = [UIFont systemFontOfSize:17];
     if (component == 0)
     {
         lable.textAlignment = NSTextAlignmentCenter;
         
         lable.text = self.hourMutableArray[row];
     }
     else if (component == 1)
     {
     
     lable.textAlignment = NSTextAlignmentLeft;
     
     lable.text =self.minuteMutableArray[row];
     }
     
     [views addSubview:lable];
     
     
     return views;
 }

@end
